<?php

namespace app\admin\service;

use app\admin\model\Clazz;
use app\admin\model\Equip;
use app\admin\model\EquipManage;
use app\admin\model\Grade;
use app\admin\model\Student;
use app\admin\model\TeacherRole;
use app\admin\model\User;

class EquipManageService extends BaseService
{
    public $equipId = 0;

    public function __construct()
    {
        $this->model = new EquipManage;
    }

    public function tree(): array
    {
        $organizationId = Equip::where('id', $this->equipId)->value('organization');
        $teacherTemp    = User::field('id, realname, teacherrole_id')
            ->where('school_id', $organizationId)
            ->where('user_type', 1)
            ->where('mark', 1)
            ->select()
            ->toArray();
        $teacher        = [];
        foreach ($teacherTemp as $val) {
            $teacher[$val['teacherrole_id']][] = ['value' => $val['id'], 'label' => $val['realname']];
        }
        $teacherRole = TeacherRole::field('id, name')
            ->where('organization', $organizationId)
            ->order('sort')
            ->select()
            ->toArray();
        $studentTemp = Student::field('id, name, grade_id, class_id')
            ->where('school_id', $organizationId)
            ->where('mark', 1)
            ->select()
            ->toArray();
        $student     = [];
        foreach ($studentTemp as $val) {
            $student[$val['grade_id']][$val['class_id']][] = ['value' => $val['id'], 'label' => $val['name']];
        }
        $classTemp = Clazz::field('id, name, grade')
            ->where('organization', $organizationId)
            ->order('sort')
            ->select()
            ->toArray();
        $class     = [];
        foreach ($classTemp as $val) {
            $class[$val['grade']][] = ['value' => $val['id'], 'label' => $val['name']];
        }
        $grade = Grade::field('id, name')
            ->where('organization', $organizationId)
            ->order('sort')
            ->select()
            ->toArray();

        $tree = [
            ['value' => 'teacher', 'label' => '教师', 'children' => []],
            ['value' => 'student', 'label' => '学生', 'children' => []],
        ];
        foreach ($teacherRole as $val) {
            $tree[0]['children'][] = [
                'value'    => $val['id'],
                'label'    => $val['name'],
                'children' => $teacher[$val['id']] ?? []
            ];
        }
        foreach ($grade as $val) {
            $classTree = [];
            foreach ($class[$val['id']] ?? [] as $v) {
                $classTree[] = [
                    'value'    => $v['value'],
                    'label'    => $v['label'],
                    'children' => $student[$val['id']][$v['value']] ?? []
                ];
            }
            $tree[1]['children'][] = [
                'value'    => $val['id'],
                'label'    => $val['name'],
                'children' => $classTree
            ];
        }

        return $tree;
    }

    public function get(): array
    {
        $manage = $this->model->where('equip_id', $this->equipId)
            ->with('manage')
            ->select()
            ->toArray();
        $data   = [];
        foreach ($manage as $val) {
            $temp = [$val['type']];
            if ($val['type'] == 'teacher') {
                $temp[] = $val['manage']['teacherrole_id'];
            } else {
                $temp[] = $val['manage']['grade_id'];
                $temp[] = $val['manage']['class_id'];
            }
            $temp[] = $val['manage']['id'];
            $data[] = $temp;
        }

        return $data;
    }

    public function save($manage)
    {
        $this->model->where('equip_id', $this->equipId)->delete();
        if (empty($manage)) {
            return;
        }
        $insert = [];
        foreach ($manage as $val) {
            $insert[] = [
                'equip_id'  => $this->equipId,
                'type'      => $val['type'],
                'manage_id' => $val['id']
            ];
        }
        $this->model->insertAll($insert);
    }
}